package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class admin_login extends Activity {
    EditText et_id;
    EditText et_password;
    CheckBox sw_load_passwd;
    CheckBox sw_auto_login;
    Button bt_login;
    Button register;
    SharedPreferences spf;
    SharedPreferences.Editor se;

    private String passwd;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_admin_login);

        et_id = (EditText) findViewById(R.id.et_id);
        et_password = (EditText) findViewById(R.id.et_password);
        sw_load_passwd = (CheckBox) findViewById(R.id.sw_load_passwd);
        sw_auto_login = (CheckBox) findViewById(R.id.sw_auto_login);
        bt_login = (Button) findViewById(R.id.bt_login);
        register = (Button)findViewById(R.id.register);
        //它是一个轻量级的存储类，特别适合用于保存软件配置参数
        spf = getSharedPreferences("Meng", MODE_PRIVATE);
        //获取编辑器
        se = spf.edit();

        //获取SharedPreferences数据指定key所对应的value，如果该key不存在，返回默认值defValue。
        String x = spf.getString("save", "0");
        String xx = spf.getString("auto", "0");

        //比较两个对象的内容是否相同(x和1)
        //记住密码
        //比较相同则是之前有保存密码和用户名
        if (x.equals("1")) {
            //当spf中x的值为1时，就是保存了用户名和密码
            sw_load_passwd.setChecked(true);
            //取得存储在存储器中的用户名和密码
            et_id.setText(spf.getString("admin_name", null));
            et_password.setText(spf.getString("admin_password", null));
        } else {
            et_id.setText("");
            et_password.setText("");
        }

        //自动登录，同上
        if (xx.equals("1")) {
            sw_auto_login.setChecked(true);

            new Thread() {
                @Override
                public void run() {
                    try {
                        sleep(2002);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Message m = new Message();
                    m.what=1;
                    myHandler.sendMessage(m);

                }
            }.start();
        }

        sw_load_passwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    //将用户名和密码写入储存器
                    se.putString("admin_name", et_id.getText().toString());
                    se.putString("admin_password", et_password.getText().toString());

                    //flag保存了
                    se.putString("save", "1");
                    se.commit();
                } else {
                    se.putString("save", "0");
                    se.commit();
                }
            }
        });

        sw_auto_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    //自动登录
                    sw_load_passwd.setChecked(true);
                    se.putString("admin_name", et_id.getText().toString());
                    se.putString("admin_password", et_password.getText().toString());

                    se.putString("save", "1");
                    se.putString("auto", "1");
                    se.commit();
                } else {
                    se.putString("auto", "0");
                    se.commit();
                }
            }
        });

        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //记住密码则将用户名和密码保存
                if (sw_load_passwd.isChecked() == true) {
                    se.putString("admin_name", et_id.getText().toString());
                    se.putString("admin_password", et_password.getText().toString());
                    se.commit();
                }

                //匹配用户名和密码，是否登录成功
                if (et_password.getText().toString().matches("[0-9a-zA-Z]{5,10}")) {
                    if (et_id.getText().toString().matches("[0-9a-zA-Z]{5,10}")) {
                        passwd = et_password.getText().toString();
                        id = et_id.getText().toString();
                            if (id.equals("123456")  && passwd.equals("123456")) {
                                Message m = new Message();
                                m.what = 4;
                                myHandler.sendMessage(m);
                            } else {
                                Message m = new Message();
                                m.what = 2;
                                myHandler.sendMessage(m);
                            }
                        } else{
                            Message m = new Message();
                            m.what = 7;
                            myHandler.sendMessage(m);
                        }
                    } else {
                        Message m = new Message();
                        m.what = 3;
                        myHandler.sendMessage(m);
                    }
            }
        });
        //用户登录按键
        bt_login.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        bt_login.performClick();
                        break;
                }

                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent i = new Intent(admin_login.this, MainActivity.class);
            startActivity(i);
            this.finish();
        }
        return true;
    }

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==1){
                bt_login.performClick();
            }
            if(msg.what==2){
                Toast.makeText(admin_login.this,"密码错误",Toast.LENGTH_LONG).show();
            };
            if(msg.what==3){
                Toast.makeText(admin_login.this,"帐号错误",Toast.LENGTH_LONG).show();
            };
            if(msg.what==4){
                Intent i = new Intent(admin_login.this, admin.class);

                startActivity(i);
                admin_login.this.finish();
            };
            if(msg.what==5){
                Toast.makeText(admin_login.this,"用户不存在",Toast.LENGTH_LONG).show();
            };
            if(msg.what==6){
                Toast.makeText(admin_login.this,"服务器没有响应",Toast.LENGTH_LONG).show();
            };
            if(msg.what==7){
                Toast.makeText(admin_login.this,"服务器断开，尝试重连",Toast.LENGTH_LONG).show();
                Socket.connector.releaseSocket();
            };
        }
    };
}
